require 'will_paginate'

class CommentsController < ApplicationController
	
	layout 'application'
	
	def create
		@picture = Picture.find(params[:picture_id])
		@comment = @picture.comments.build(params[:comment])
		@comment.user_id = session[:user_id]
		@comment.save
		#redirect_to :back
		respond_to do |format|
			format.html { redirect_to :back }
			format.js
		end
	end

	def update
		@comment = Comment.find(params[:id])
		
		respond_to do |format|
			if @comment.body == nil
				flash[:notice] = "Komentarz nie może być pusty !!!"
			else
				@comment.update_attributes(params[:comment])
				format.html { redirect_to ( :controller => "picture", :action => "show", :id => @comment.picture_id ) } 
			end
		end
	end
	
	def edit
		@title = "Edytuj komentarz"
		
	end

	def remove
		@comment = Comment.find(params[:id])
		@picture = @comment.picture_id
		@comment.destroy

		respond_to do |format|
			format.html { redirect_to ( :controller => "picture", :action => "show", :id => @picture )  }
		end
	end 
end
